package com.zycfc.zsf.boot.mybatis.typehandler;

import com.fasterxml.jackson.databind.*;
import com.zycfc.zsf.boot.util.json.*;
import java.lang.reflect.*;
import org.apache.ibatis.type.*;
import java.sql.*;
import org.slf4j.*;

public abstract class AbstractJsonTypeHandler<T> extends BaseTypeHandler<T>
{
    private static final Logger log;
    private final JavaType javaType;
    
    public AbstractJsonTypeHandler() {
        this.javaType = JsonUtils.getJavaType(this.getType());
    }
    
    public final Type getType() {
        final Type superClass = this.getClass().getGenericSuperclass();
        if (superClass instanceof Class) {
            throw new IllegalArgumentException("\u5fc5\u987b\u8bbe\u7f6eAbstractJsonTypeHandler\u4e2d\u7684\u6cdb\u578b\u7c7b\u578b");
        }
        final Type type = ((ParameterizedType)superClass).getActualTypeArguments()[0];
        if (type.getTypeName().equals(Object.class.getName())) {
            throw new IllegalArgumentException("AbstractJsonTypeHandler\u4e2d\u7684\u6cdb\u578b\u7c7b\u578b\u4e0d\u80fd\u662fObject");
        }
        return type;
    }
    
    public void setNonNullParameter(final PreparedStatement ps, final int i, final T parameter, final JdbcType jdbcType) throws SQLException {
        ps.setString(i, JsonUtils.objectToJson((Object)parameter));
    }
    
    public T getNullableResult(final ResultSet rs, final String columnName) throws SQLException {
        final String json = rs.getString(columnName);
        if (rs.wasNull()) {
            return null;
        }
        try {
            return (T)JsonUtils.readValue(json, this.javaType);
        }
        catch (Exception e) {
            AbstractJsonTypeHandler.log.error(e.getMessage(), (Throwable)e);
            return null;
        }
    }
    
    public T getNullableResult(final ResultSet rs, final int columnIndex) throws SQLException {
        final String json = rs.getString(columnIndex);
        if (rs.wasNull()) {
            return null;
        }
        try {
            return (T)JsonUtils.readValue(json, this.javaType);
        }
        catch (Exception e) {
            AbstractJsonTypeHandler.log.error(e.getMessage(), (Throwable)e);
            return null;
        }
    }
    
    public T getNullableResult(final CallableStatement cs, final int columnIndex) throws SQLException {
        final String json = cs.getString(columnIndex);
        if (cs.wasNull()) {
            return null;
        }
        try {
            return (T)JsonUtils.readValue(json, this.javaType);
        }
        catch (Exception e) {
            AbstractJsonTypeHandler.log.error(e.getMessage(), (Throwable)e);
            return null;
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)AbstractJsonTypeHandler.class);
    }
}
